//
//  CTCAnswerQuestionVC.m
//  ConsultantVersion
//
//  Created by 李小龙 on 2017/3/28.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "CTCAnswerQuestionVC.h"

@interface CTCAnswerQuestionVC ()

@property (weak, nonatomic) IBOutlet BWHTextView *answerTextView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewHeightLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *answerBtnHeightLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoViewHeightLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *photoBackViewTopLayout;

@property (weak, nonatomic) IBOutlet HZPhotoGroup *photoGroup;

@end

@implementation CTCAnswerQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    
    [self setupUI];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.bottomViewHeightLayout.constant = 0;
    self.answerBtnHeightLayout.constant = 0;
    
    if (!self.questionModel || !self.questionModel.imgs || !self.questionModel.imgs.length) {
        _photoBackViewTopLayout.constant = 0;
        _photoGroup.photoItemArray = @[];
        [_photoGroup setHeight:0];
        _photoViewHeightLayout.constant = 0;
        return;
    }
    
    NSMutableArray *images = [@[] mutableCopy];
    
    NSArray *arr = [_questionModel.imgs componentsSeparatedByString:@","];
    
    [arr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [images addObject:[NSString stringWithFormat:@"%@/%@/%@", kMainBaseUrl, kShowImage, obj]];
    }];
    
    _photoGroup.photoItemArray = images;
    _photoViewHeightLayout.constant = _photoGroup.height;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavBar
{
    self.title = @"回答问题";
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
    
}


- (void)setupUI
{
    self.answerTextView.placeHolderText = @"请输入您的回答......";
    
    self.contentLabel.text = self.questionModel.content;
    self.nameLabel.text = self.questionModel.name;
    self.dateLabel.text = [NSDate stringFromTimestamp:self.questionModel.date];
    [SDWebImageTool imageView:self.headImageView imageName:self.questionModel.headImg placeHolder:@"default-head" showProgress:NO];
}


- (void)rightBarButtonItemClick
{
    
    if (!self.answerTextView || !self.answerTextView.text.length) {
        [MBProgressHUD showTipMessageInView:@"请输入您的回答"];
        return;
    }
    
    
    [self.view endEditing:YES];
    
    NSDictionary *paramDic = @{
                               @"uuid":self.questionModel.uuid,
                               @"content":self.answerTextView.text
                               };
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:kSubmoitRespondeAnswerUrl
           parameters:paramDic
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"responseObject %@", responseObject);
                  
                  NSInteger code = [responseObject[@"code"] integerValue];
                  NSString *des = responseObject[@"des"];
                  if (code == 1) {
                      [MBProgressHUD showSuccessMessage:des];
                      [[NSNotificationCenter defaultCenter] postNotificationName:kReloadIndexPathSelectedRepaidListNotification object:nil];;
                      
                      if (self.isRespoderNoAnswer) {
                          
                          [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                          
                          if (self.answerSuccess) {
                              self.answerSuccess();
                          }
                      }
                      else {
                          [self.navigationController popToRootViewControllerAnimated:YES];
                      }
                      
                  }
                  else {
                     [MBProgressHUD showErrorMessage:des];
                      
                      if (code == 2) {
                          CTCLoginViewController *vc = [CTCLoginViewController viewControllerFromStoryboard];
                          [self presentViewController:vc animated:YES completion:nil];
                      }
                     
                  }
              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"error %@", error);
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力，请检查网络"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }
              }];
}



- (IBAction)answerBtnClick {
    
}


@end
